#include "cpp-httplib/httplib.h"
#include "searcher.hpp"
 
const std::string root_path = "./wwwroot";
const std::string input = "data/cln_html/cln.txt";

int main()
{
    ns_searcher::Searcher search;
    search.InitSearcher(input);
    httplib::Server svr;

    svr.set_base_dir(root_path.c_str()); //设置web根目录
    //以Get的方式给client发送信息
    svr.Get("/s",[&search](const httplib::Request &req, httplib::Response &rsp){
        if (!req.has_param("word"))
        {
            rsp.set_content("必须要有搜索关键字!", "text/plain; charset=utf-8");
            return;
        }
        std::string word = req.get_param_value("word"); // 提取搜索关键字
        std::cout << "用户正在搜索: " << word << std::endl;
        std::string json_string;
        search.Search(word, &json_string);
        rsp.set_content(json_string, "application/json");
        //rsp.set_content("你好,世界!", "text/plain; charset=utf-8");
    });
 
	// 设置监听
    svr.listen("0.0.0.0", 7777);
 
    return 0;
}